package com.polonium.pageobject.utils;

import org.apache.commons.io.IOUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URL;
import java.util.Properties;

public class ConfigReader {

//	private static final Logger LOG = LoggerFactory.getLogger(ConfigReader.class);

	private Properties properties;

	public ConfigReader(String propertiesFileName) {
//		Verify.isNotNull(propertiesFileName);
		properties = init(propertiesFileName);
	}

	private Properties init(String propertiesFileName) {
		Properties result = new Properties();
		FileInputStream inStream = null;
		try {
			URL url = ClassLoader.getSystemResource(propertiesFileName);
			if (url == null) {
				throw new IOException();
			}
			inStream = new FileInputStream(new File(url.getFile()));
			result.load(inStream);
		} catch (IOException e) {
//			LOG.info("No properties file found");
		} finally {
			IOUtils.closeQuietly(inStream);
		}
		return result;
	}

	public String getProperty(String name, String defaultValue) {
		return properties.getProperty(name, defaultValue);
	}

	public Integer getProperty(String name, Integer defaultValue) {
		String property = properties.getProperty(name);
		Integer value = defaultValue;
		try {
			value = Integer.parseInt(property);
		} catch (NumberFormatException e) {
//			LOG.warn("Could not parse: " + property, e);
		}
		return value;
	}

	public Boolean getProperty(String name, Boolean defaultValue) {
		return defaultValue;
	}

	public Long getProperty(String name, Long defaultValue) {
		return defaultValue;
	}
}
